其他
Python脚本打包下载必应壁纸2009-2021
有图必应 | 必应壁纸 -探索更多精彩 https://bing.wilii.cn/index.html
http://chfs3.chenyanggao.top:49414/#/%F0%9F%96%BC%EF%B8%8FBing%E5%A3%81%E7%BA%B8
脚本与使用方法
#!/usr/bin/env python3
# coding: utf-8
__author__ = 'ChenyangGao <https://chenyanggao.github.io/>'
__version__ = (0, 0, 1)
import logging
from functools import partial, update_wrapper
from json import loads
from os import path, remove
from shutil import copyfileobj
from urllib.parse import urlencode, urlparse
from urllib.request import urlopen, Request
logging.basicConfig(
level=logging.INFO,
format="[\x1b[1m%(asctime)-15s\x1b[0m] \x1b[36;1m%(name)s\x1b[0m"
"(\x1b[31;1m%(levelname)s\x1b[0m) ➜ %(message)s"
)
def retry(fn=None, times=5, exceptions=Exception):
if fn is None:
return partial(retry, times=times, exceptions=exceptions)
def wrapper(*args, **kwds):
excs = []
for _ in range(times):
try:
return fn(*args, **kwds)
except exceptions as e:
excs.append(e)
raise Exception(excs)
return update_wrapper(wrapper, fn)
@retry
def get_json(url, params=None):
if params:
url = url + '?' + urlencode(params)
with urlopen(url) as resp:
return loads(resp.read())
@retry
def download(req, filename=None, **kwds):
with urlopen(req, **kwds) as resp:
if filename is None:
hdr_cd = resp.headers.get('Content-Disposition')
if hdr_cd and hdr_cd.startswith('attachment; filename='):
filename = hdr_cd[21:]
else:
url = req.full_url if isinstance(req, Request) else req
filename = urlparse(url).path.rsplit('/', 1)[-1]
try:
copyfileobj(resp, open(filename, 'wb'))
except:
remove(filename)
raise
def wilii_page(
pagenumber=1,
pagesize=12,
dictType='DayList2',
query='',
**params,
):
url = 'https://api.wilii.cn/bing/binglist.ashx'
params = {
'pagenumber': pagenumber,
'pagesize': pagesize,
'dictType': dictType,
'query': query,
**params,
}
return get_json(url, params=params)
def wilii_item(
id,
DictType='Detail',
**params,
):
url = 'https://api.wilii.cn/bing/binglist.ashx'
params = {
'id': id,
'DictType': DictType,
**params,
}
js = get_json(url, params)
if js:
return js[0]
def download_wilii_item(iteminfo, dir=''):
try:
yyyy = iteminfo['yyyy']
mm = iteminfo['mm']
dd = iteminfo['dd']
pic_urlpic = iteminfo['pic_urlpic'][1:]
if iteminfo['type'] == 0:
suffix = '_1920x1080.jpg'
elif iteminfo['type'] == 1:
suffix = '_1366x768.jpg'
else:
suffix = '.jpg'
except KeyError:
logging.error('\x1b[33;1m[信息不足]\x1b[0m %r' % iteminfo)
raise
name = pic_urlpic + suffix
filename = '%s-%s-%s_%s' % (yyyy, mm, dd, name)
link = 'https://bing.wilii.cn/OneDrive/bingimages/%s/%s/%s/%s' % (yyyy, mm, dd, name)
try:
download(link, path.join(dir, filename))
logging.info('\x1b[32;1m[成功]\x1b[0m %s' % link)
except Exception:
logging.error('\x1b[33;1m[失败]\x1b[0m %s' % link)
raise
if __name__ == '__main__':
from argparse import ArgumentParser
from concurrent.futures import ThreadPoolExecutor
ap = ArgumentParser(description='采集 https://bing.wilii.cn 上的必应壁纸')
ap.add_argument('-a', '--begin', type=int, default=1, help='开始页,默认是 1')
ap.add_argument('-z', '--end', type=int, default=1 << 100, help='结束页,默认是最后一页')
ap.add_argument('-s', '--save-dir', dest='save_dir', default='', help='保存在这个文件夹')
args = ap.parse_args()
save_dir = args.save_dir
if save_dir not in ('', '.'):
from os import makedirs
makedirs(save_dir, exist_ok=True)
begin = args.begin
end = args.end
with ThreadPoolExecutor() as exe:
for i in range(begin, end + 1):
ls = wilii_page(i)['list']
if not ls:
break
for item in ls:
exe.submit(download_wilii_item, item, save_dir)
python .\bing_wilii.py -h
usage: bing_wilii.py [-h] [-a BEGIN] [-z END] [-s SAVE_DIR]
采集 https://bing.wilii.cn 上的必应壁纸
optional arguments:
-h, --help show this help message and exit
-a BEGIN, --begin BEGIN
开始页,默认是 1
-z END, --end END 结束页,默认是最后一页
-s SAVE_DIR, --save-dir SAVE_DIR
保存在这个文件夹
壁纸欣赏
点赞、转发、广告,更多优质资源等你来...
(必应壁纸)
数学知识地图【中英字幕】The Map of Math
走向数学丛书(全18册)
《久坐不伤身》读书笔记:检查你的坐姿